package com.example.book.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.book.entity.Orders;
import com.example.book.entity.Products;
import com.example.book.entity.User;
import com.example.book.service.OrdersService;
import com.example.book.service.ProductsService;
import com.example.book.service.UserService;
import com.example.book.util.BookStoreUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.SecureRandom;
import java.util.*;

@Controller
@RequestMapping("/book")
public class PageController {


    @Autowired
    private ProductsService productsService;

    @Autowired
    private OrdersService ordersService;


    @Autowired
    private UserService userService;

    //订单页
    @RequestMapping("/order")
    public String order(String username,HttpServletRequest request,Model model){

        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("receiverName",username);
        List<Orders> list = ordersService.list(queryWrapper);
        model.addAttribute("username_order",list);


        return "pages/order/order";
    }



    //购物车页
    @RequestMapping("/cart")
    public String cart(String username,HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        Object attribute = session.getAttribute(username + "_cart");

        List<Products> cart;
        if(attribute==null){
            cart=null;
            model.addAttribute("cart",cart);
        }else{
            double _cart_totalCount = (double)session.getAttribute(username + "_cart_totalCount");
            double _cart_totalPrice = (double)session.getAttribute(username + "_cart_totalPrice");
            Map<String,Double> resMap=new HashMap<>();
            resMap.put("totalCount",_cart_totalCount);
            resMap.put("totalPrice",_cart_totalPrice);
            cart=(List<Products>) attribute;
            model.addAttribute("cart",cart);
            model.addAttribute("toTalPriceAndTotalCount",resMap);
        }

        return "pages/cart/cart";
    }

    //结算页
    @RequestMapping("/checkout")
    public String checkout(String username,HttpServletRequest request,Model model){


        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("username",username);
        User user = userService.getOne(userQueryWrapper);


        Date date=new Date();
        long orderId=date.getTime();
        model.addAttribute("orderId",orderId);


        Orders orders=new Orders();
        orders.setOrdertime(date);
        orders.setMoney(((double)request.getSession().getAttribute(username+"_cart_totalPrice")));
        orders.setReceiverAddress(user.getIntroduce());
        orders.setUserId(user.getId());
        orders.setReceiverName(user.getUsername());
        orders.setReceiverPhone(user.getTelephone());
        ordersService.save(orders);

        request.getSession().removeAttribute(username+"_cart");
        return "pages/cart/checkout";
    }
    //清空页
    @RequestMapping("/clearout")
    public String clearout(String username,HttpServletRequest request,Model model){
        request.getSession().removeAttribute(username+"_cart");
        return "pages/cart/cart";
    }




    @RequestMapping("/loginClose")
    public String loginClose(String username,HttpServletRequest request,Model model){
        request.getSession().removeAttribute("user");
        return this.index(1,"",model);
    }



    //主页
    @RequestMapping("/index")
    public String index(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                        @RequestParam(value = "category", defaultValue = "", required = false) String category,
                        Model model){


        QueryWrapper<Products> queryWrapper=new QueryWrapper<>();
        if(category==null||category.equals("")){
            queryWrapper
                    .select();
        }else{
            queryWrapper
                    .select()
                    .eq("category",category);
        }

        PageHelper.startPage(pageNum, 4);
        List<Products> books = productsService.list(queryWrapper);
        PageInfo pageInfo = BookStoreUtils.getPageInfo(books, 5);
        model.addAttribute("pageInfo", pageInfo);
        return "index";
    }

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/register")
    public String register(){
        return "register";
    }



}
